package com.immunology.logic.validators;

public class NameSurmameFieldsValidator extends FieldValidator {

	private Alphabet alphabet = new Alphabet();

	public NameSurmameFieldsValidator() {
		super();
	}

	public boolean validate(String nameOrSurname) {
		boolean result = false;

		boolean valid1 = validateFirstLetterByNotFirstLetterSymbol(nameOrSurname);
		boolean valid2 = super.validate(nameOrSurname,
				alphabet.getFullAlphabet());
		boolean valid3 = isManyApostrophes(nameOrSurname);
		result = valid1 & valid2 & valid3;

		return result;
	}

	private boolean validateFirstLetterByNotFirstLetterSymbol(
			String nameOrSurname) {
		boolean result = true;
		if (super.isFirstLetterInAlphabet(nameOrSurname,
				alphabet.getNotFirstLetterSymbol())) {
			result = false;
		}
		return result;
	}

	public String refactorNameOrSurname(String nameOrSurname) {
		String result = "";
		if (!nameOrSurname.isEmpty()) {
			if (!super.isFirstLetterInAlphabet(nameOrSurname,
					alphabet.getAllHighLetter())) {
				result = new StringBuilder()
						.append(nameOrSurname.substring(0, 1).toUpperCase())
						.append(nameOrSurname.substring(1,
								nameOrSurname.length())).toString();
			} else {
				result = nameOrSurname;
			}
		}
		return result;
	}

	private boolean isManyApostrophes(String nameOrSurname) {
		boolean result = true;
		if (super.countApostrophes(nameOrSurname) > 1) {
			result = false;
		}
		return result;
	}
}
